﻿// Test client for RESTful web service

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;

namespace RetaurantClient
{
    // Reservations class
    public class Reservation
    {
        // Date of reservation
        public DateTime Date { get; set; }

        // Name of reservation
        public string Name { get; set; }

        // Phone Number of reservation
        public string PhoneNumber { get; set; }

        // Number of seats of reservation
        public int Seats { get; set; }
    }
 
  // DailySummary class
    public class DailySummary
    {
        public DateTime Date { get; set; }
        public int NumReservations { get; set; }
        public int NumSeats { get; set; }

    }

    // test
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://xnumber.azurewebsites.net/");                             // base URL for API Controller i.e. RESTFul service

                // add an Accept header for JSON
                client.DefaultRequestHeaders.
                    Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // GET ../api/webservice
                // get reservations for today
                HttpResponseMessage response = client.GetAsync("api/webservice/").Result;                 
                if (response.IsSuccessStatusCode)                                                 
                {
                    Console.WriteLine("This weeks reservations: ");
                    int a = 0;
                    var dailySummary = response.Content.ReadAsAsync<IEnumerable<DailySummary>>().Result;
                    foreach (var dailysummary in dailySummary)
                    {
                        Console.WriteLine(a + " - " + dailysummary.Date + " there are " + dailysummary.NumReservations + " Reservations for " + dailysummary.NumSeats + " people.");
                        a++;
                    }
                    Console.WriteLine();
                    
                }
                else
                {
                    Console.WriteLine(response.StatusCode + " " + response.ReasonPhrase);
                }

                Console.WriteLine("Select a number to choose a date for more detailed info on reservations");
                int day = int.Parse(Console.ReadLine());

                DateTime queryDate = DateTime.Now.AddDays(day);
                string format = "yyyy-MM-dd";       //format date to work with Web API service
                
                response = client.GetAsync("api/webservice/" + queryDate.ToString(format)).Result;
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(queryDate.ToString(format) + " reservations: ");
                    var reservations = response.Content.ReadAsAsync<IEnumerable<Reservation>>().Result;
                    foreach (var reservation in reservations)
                    {
                        Console.WriteLine(reservation.Name + " Table for " + reservation.Seats + ", Phone Number: " + reservation.PhoneNumber);
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(response.StatusCode + " " + response.ReasonPhrase);
                }             
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}
